import categoryApi from "@/api/category";
const state = {
  category1Id: undefined,
  category1List: [],
  category2Id: undefined,
  category2List: [],
  category3Id: undefined,
  category3List: [],
};

const mutations = {
  SET_CATEGORY1ID(state, id) {
    state.category1Id = id
  },
  SET_CATEGORY2ID(state, id) {
    state.category2Id = id
  },
  SET_CATEGORY3ID(state, id) {
    state.category3Id = id
  },
  SET_CATEGORY1LIST(state, category1List) {
    state.category1List = category1List
  },
  SET_CATEGORY2LIST(state, category2List) {
    state.category2List = category2List
  },
  SET_CATEGORY3LIST(state, category3List) {
    state.category3List = category3List
  }
};

const actions = {
  async getCategory1({ commit }) {
    try {
      let result = await categoryApi.getCategory1();
      commit('SET_CATEGORY1LIST', result.data)
    } catch (error) {
      console.error(error)
    }
  },
  async getCategory2({ commit, state }) {
    state.category2Id = undefined;
    state.category3Id = undefined;
    state.category2List = [];
    state.category3List = [];
    try {
      let result = await categoryApi.getCategory2(state.category1Id);
      commit('SET_CATEGORY2LIST', result.data)
    } catch (error) {
      console.error(error)
    }
  },
  async getCategory3({ commit, state }) {
    state.category3Id = undefined;
    state.category3List = [];
    try {
      let result = await categoryApi.getCategory3(state.category2Id);
      commit('SET_CATEGORY3LIST', result.data)
    } catch (error) {
      console.error(error)
    }
  }
};

const getters = {

};
export default {
  state,
  mutations,
  actions,
  getters,
  namespaced: true,
}